/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web.business;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;

import com.thinkgem.jeesite.modules.sys.entity.defaulttime.DefaultTime;
import com.thinkgem.jeesite.modules.sys.entity.member.Member;
import com.thinkgem.jeesite.modules.sys.service.defaulttime.DefaultTimeService;
import com.thinkgem.jeesite.modules.sys.service.member.MemberService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.business.Business;
import com.thinkgem.jeesite.modules.sys.service.business.BusinessService;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * businessController
 * @author jin
 * @version 2018-04-10
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/business/business")
public class BusinessController extends BaseController {

	@Autowired
	private BusinessService businessService;
	@Autowired
	private DefaultTimeService defaultTimeService;
    @Autowired
    private MemberService memberService;
	
	@ModelAttribute
	public Business get(@RequestParam(required=false) String id) {
		Business entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = businessService.get(id);
		}
		if (entity == null){
			entity = new Business();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:business:business:view")
	@RequestMapping(value = {"list", ""})
	public String list(Business business, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Business> page = businessService.findPage(new Page<Business>(request, response), business); 
		model.addAttribute("page", page);
		return "modules/sys/business/businessList";
	}

	@RequiresPermissions("sys:business:business:view")
	@RequestMapping(value = "list2")
	public String list2(Business business, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Business> page = businessService.findPage2(new Page<Business>(request, response), business);
		model.addAttribute("page", page);
		return "modules/sys/business/businessList2";
	}

	@RequiresPermissions("sys:business:business:view")
	@RequestMapping(value = "getYj")
	public String list3(Business business, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Business> page = businessService.findPage3(new Page<Business>(request, response), business);
		model.addAttribute("page", page);
		return "modules/sys/business/getYj";
	}

	@RequiresPermissions("sys:business:business:view")
	@RequestMapping(value = "form")
	public String form(Business business, Model model) {
		model.addAttribute("business", business);
		return "modules/sys/business/businessForm";
	}

	@RequiresPermissions("sys:business:business:edit")
	@RequestMapping(value = "save")
	public String save(Business business, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, business)){
			return form(business, model);
		}
		businessService.save(business);
		addMessage(redirectAttributes, "保存业务成功");
		return "redirect:"+Global.getAdminPath()+"/sys/business/business/?repage";
	}
	
	@RequiresPermissions("sys:business:business:edit")
	@RequestMapping(value = "delete")
	public String delete(Business business, RedirectAttributes redirectAttributes) {
		businessService.delete(business);
		addMessage(redirectAttributes, "删除业务成功");
		return "redirect:"+Global.getAdminPath()+"/sys/business/business/?repage";
	}

	@RequestMapping(value = "huifang")
	public String huifang(String id,Model model){
		Business business = businessService.get(id);
		DefaultTime defaultTime = defaultTimeService.get("1");
		model.addAttribute("business",business);
		model.addAttribute("defaultTime",defaultTime);
		return "modules/sys/business/huiFang";
	}
    @ResponseBody
    @RequestMapping(value = "saveHuiFang")
    public Object saveHuiFang(Business business){
	    String ssd = business.getHuiDay();
	    DefaultTime defaultTime = defaultTimeService.get("1");
	    if(business.getHfTime() ==null){ business.setHfTime(new Date());}
	    if(business.getHuiDay() ==null || business.getHuiDay().isEmpty()){business.setHuiDay(defaultTime.getHuiDay());}
        if(business.getId()!= null || business.getHfTime()!=null || business.getHuiDay()!=null){
            business.setYjStatic("2");
            Format f = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(business.getHfTime());
            int num = Integer.valueOf(business.getHuiDay());
            c.add(Calendar.DAY_OF_MONTH, num);
            Date da = c.getTime();
            business.setHffinalTime(da);
            businessService.save(business);
        return true;
        }else {return false;}
    }

	@RequestMapping(value = "shouli")
	public String shouli(String id,Model model){
		Business business = businessService.get(id);
		DefaultTime defaultTime = defaultTimeService.get("1");
		model.addAttribute("business",business);
		model.addAttribute("defaultTime",defaultTime);
		return "modules/sys/business/shouLi";
	}
	@ResponseBody
	@RequestMapping(value = "saveShouLi")
	public Object saveShouLi(Business business){
		String ssd = business.getSlDay();
		DefaultTime defaultTime = defaultTimeService.get("1");
//		if((business.getSlDay() ==null || business.getSlDay().isEmpty()) &&business.getDifficulty().equals("简易")){business.setSlDay(defaultTime.getSlJian());}
//		if((business.getSlDay() ==null || business.getSlDay().isEmpty()) && business.getDifficulty().equals("复杂")){business.setSlDay(defaultTime.getSlFu());}
		if(business.getId()!= null || business.getDifficulty()!=null){
			business.setYjStatic("3");
			business.setHfStime(new Date());
			business.setSlTime(new Date());
			Format f = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			c.setTime(business.getSlTime());
			int num = Integer.valueOf(business.getSlDay());
			c.add(Calendar.DAY_OF_MONTH, num);
			Date da = c.getTime();
			business.setFinaTime(da);
			businessService.save(business);
			return true;
		}else {return false;}
	}

	@RequestMapping(value = "jiean")
	public String jiean(String id,Model model){
		Business business = businessService.get(id);
		model.addAttribute("business",business);
		return "modules/sys/business/jieAn";
	}

	@ResponseBody
	@RequestMapping(value = "saveJieAn")
	public Object saveJieAn(Business business){
		business.setJieTime(new Date());
		business.setYjStatic("7");
		businessService.save(business);
		return true;
	}

	@RequestMapping(value = "diaocha")
	public String diaocha(String id,Model model){
		Business business = businessService.get(id);
		DefaultTime defaultTime = defaultTimeService.get("1");
		model.addAttribute("business",business);
		model.addAttribute("defaultTime",defaultTime);
		return "modules/sys/business/diaoCha";
	}

	@ResponseBody
	@RequestMapping(value = "saveDiaocha")
	public Object saveDiaocha(Business business){
		DefaultTime defaultTime = defaultTimeService.get("1");
		if(business.getDcTime() == null){
			business.setDcTime(new Date());
		}
		if(business.getDcDay() == null || business.getDcDay().isEmpty()){
//			business.setDcDay(defaultTime.getDcDay());
		}
		if(business.getId()!= null){
			business.setYjStatic("4");
			Format f = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			c.setTime(business.getDcTime());
			int num = Integer.valueOf(business.getDcDay());
			c.add(Calendar.DAY_OF_MONTH, num);
			Date da = c.getTime();
			business.setDcFtime(da);
			businessService.save(business);
			return true;
		}else {return false;}
	}

	@RequestMapping(value = "xietiao")
	public String xietiao(String id,Model model){
		Business business = businessService.get(id);
		DefaultTime defaultTime = defaultTimeService.get("1");
		model.addAttribute("business",business);
		model.addAttribute("defaultTime",defaultTime);
		return "modules/sys/business/xieTiao";
	}

	@ResponseBody
	@RequestMapping(value = "saveXietiao")
	public Object saveXietiao(Business business){
		DefaultTime defaultTime = defaultTimeService.get("1");
		if(business.getXtTime() == null){
			business.setXtTime(new Date());
		}
		if(business.getXtDay() == null || business.getXtDay().isEmpty()){
//			business.setXtDay(defaultTime.getXtDay());
		}
		if(business.getId()!= null){
			business.setDcStime(new Date());
			business.setYjStatic("5");
//			Format f = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			c.setTime(business.getXtTime());
			int num = Integer.valueOf(business.getXtDay());
			c.add(Calendar.DAY_OF_MONTH, num);
			Date da = c.getTime();
			business.setXtFtime(da);
			businessService.save(business);
			return true;
		}else {return false;}
	}

	@RequestMapping(value = "shenpi")
	public String shenpi(String id,Model model){
		Business business = businessService.get(id);
		DefaultTime defaultTime = defaultTimeService.get("1");
		model.addAttribute("business",business);
		model.addAttribute("defaultTime",defaultTime);
		return "modules/sys/business/shenPi";
	}
	@ResponseBody
	@RequestMapping(value = "saveShenpi")
	public Object saveShenpi(Business business){
		DefaultTime defaultTime = defaultTimeService.get("1");
		if(business.getSpTime() == null){
			business.setSpTime(new Date());
		}
		if(business.getSpDay() == null || business.getSpDay().isEmpty()){
//			business.setSpDay(defaultTime.getSpDay());
		}
		if(business.getId()!= null){
			business.setXtStime(new Date());
			business.setYjStatic("6");
			Format f = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			c.setTime(business.getXtTime());
			int num = Integer.valueOf(business.getSpDay());
			c.add(Calendar.DAY_OF_MONTH, num);
			Date da = c.getTime();
			business.setSpFtime(da);
			businessService.save(business);
			return true;
		}else {return false;}
	}

    @RequestMapping(value = "xinxi")
    public String xinxi(String id,Model model){
        Business business = businessService.get(id);
        Member member = memberService.get(business.getMemberId());
        model.addAttribute("business",business);
        model.addAttribute("member",member);
        return "modules/sys/business/xinXi";
    }

    @RequestMapping(value = "kfTongji")
    public String kfTongji(String kfId,Model model){
            String id = kfId;
            List<Map<String,Object>> maps = new ArrayList<Map<String, Object>>();
           Map<String,Object> ma1 = new HashMap<String,Object>();
            ma1.put("1","0");
            ma1.put("2","0");
            ma1.put("3","0");
            ma1.put("4","0");
            ma1.put("5","0");
            ma1.put("6","0");
            ma1.put("7","0");
            maps=businessService.kfTj(kfId);
            if(maps.size()>0 || maps != null){
                for (Map<String,Object> item:maps){
                    ma1.put(item.get("zt").toString(), item.get("num").toString());
                }
            }
            model.addAttribute("kfId",id);
            model.addAttribute("maps",maps);
            model.addAttribute("map1",ma1);
        return "modules/sys/business/kfmap";
    }

	@ResponseBody
	@RequestMapping(value = "getkfMaps")
	public Object saveDiaocha(String kfId){
		List<Map<String,Object>> maps = new ArrayList<Map<String, Object>>();
		Map<String,Object> ma1 = new HashMap<String,Object>();
		ma1.put("1","0");
		ma1.put("2","0");
		ma1.put("3","0");
		ma1.put("4","0");
		ma1.put("5","0");
		ma1.put("6","0");
		ma1.put("7","0");
		maps=businessService.kfTj(kfId);
		if(maps.size()>0 || maps != null){
			for (Map<String,Object> item:maps){
				ma1.put(item.get("zt").toString(), item.get("num").toString());
			}
		}
		return ma1;
	}

}